/**
 * Created by leiweibo on 6/28/15.
 */
var app = angular.module('officecontroller', []);

app.filter('offset', function () {
  return function (input, start) {
    start = parseInt(start, 10);
    return input.slice(start);
  };
});

app.controller('OfficeCtrl', function ($scope, $window, ProvinceService, HospitalService, OfficeService, OfficeData) {
  $scope.callServer = function(tableState) {
    $scope.isLoading = true;

    var pagination = tableState.pagination;
    var pageSize = pagination.number || 20;
    var pageNo = Math.ceil((pagination.start + 1) / pageSize) || 1;

    OfficeService.list(pageNo, pageSize, tableState, function(res) {
      $scope.displayed = res.offices;
      tableState.pagination.numberOfPages = res.pageCount;
      $scope.isLoading = false;
    });

  };

  $scope.role = $window.sessionStorage.role;
  $scope.hospital = $window.sessionStorage.hospital;
  $scope.province = $window.sessionStorage.province;

  console.log($scope.role);
  console.log($scope.hospital);
  console.log($scope.province);

  $scope.provinceChanged = function(provinceId) {
    $scope.hospitals = HospitalService.list_by_province(provinceId, function(res) {
      if (res.result == false) {
        $scope.error_msg = res.message;
      }
      $scope.hospitals = res.hospitals;
    });
  }

  $scope.provinces = ProvinceService.list(function(data) {
    $scope.provinces = data.provinces
  });

  $scope.add = function() {
    OfficeData.set(null);
    window.location = "#/create_office";
  }

  $scope.edit = function(pos) {
    OfficeData.set($scope.displayed[pos]);
    window.location = "#/update_office";
  }

  if (OfficeData.get() && !$.isEmptyObject(OfficeData.get())) {
    var data = OfficeData.get();
    $scope.id = data._id;
    $scope.name = data.name;

    HospitalService.list_by_province(data.province._id, function(res) {
      if (res.result == false) {
        $scope.error_msg = res.message;
      }
      $scope.hospitals = res.hospitals;
    });

    $scope.province = data.province._id;
    $scope.hospital = data.hospital._id;
    $scope.introduce = data.introduce;
  }

  $scope.delete = function (pos) {
    bootbox.setDefaults("locale", "zh_CN");
    bootbox.confirm("确定删除该科室的信息吗", function (result) {
      if (result == true) {
        var office = $scope.displayed[pos];
        if (office) {
          var data = {id: office._id};
          OfficeService.delete(data, function (res) {
            if (res.result == true) {
              $scope.displayed.splice(pos, 1);
            } else {
              alert('删除数据失败');
            }
          }, function (data, status) {
          });
        } else {
          alert("请刷新重试");
        }
      }
    });
  };


  $scope.createOrUpdate = function() {
    var formData = {
      id: $scope.id,
      name: $scope.name,
      introduce: $scope.introduce
    }

    if ($scope.role == 4) {
      formData.province = $scope.province;
      formData.hospital = $scope.hospital;
    } else if ($scope.role == 1) {
      formData.province = $scope.province;
      formData.hospital = $scope.hospital;
    } else {
      return;
    }

    if ($scope.id) {
      OfficeService.update(formData, function (res) {
        if (res.result == true) {
          window.location = "#/office_list";
        } else {
          $scope.error_msg = res.message;
        }
      }, function (data, status) {
        if (status == 403) {
          $scope.error_msg = '无权限进行此操作';
        }

      })
    } else {
      OfficeService.create(formData, function(res) {
        if (res.result == true) {
            delete $scope.id;
            delete $scope.name;
            delete $scope.error_msg;
            if ($scope.role == 1) {
              delete $scope.province;
              delete $scope.hospital;
            }
            delete $scope.introduce;
        } else {
          $scope.error_msg = res.message;
        }
      }, function(data, status) {
        $scope.error_msg = '无权限进行此操作';
      })
    }
  }

  $scope.cancel = function() {
    window.location = '#/office_list';
  }
})
